//
//  ReNewAlbumViewController.m
//  MusicPlayer
//
//  Created by dlios on 15-5-25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ReNewAlbumViewController.h"
#import "ReNewAlbumLayout.h"
#import "ReNewAlbumCollectionViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "RecommendLatestModel.h"
#import "ReNewMusicChineseViewController.h"
#import "ReHotSongListViewController.h"
@interface ReNewAlbumViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIImageView *myImageView;
@end

@implementation ReNewAlbumViewController
- (void)dealloc
{
    [_array release];
    [_collectionView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}



-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.collectionView];
    
    
}



- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    self.navigationItem.title = @"最新专辑";
    [self connect];
    [self createCollectionView];
}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 创建collectionView
- (void)createCollectionView
{
    ReNewAlbumLayout *layout = [[ReNewAlbumLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    // 注册重用池
    [self.collectionView registerClass:[ReNewAlbumCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    [layout release];
    [self.collectionView release];
}

#pragma mark - 使用AFNetworking网络连接
- (void)connect
{
    // 解析tableHeaderView的网络数据
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSString *str = [NSString stringWithFormat:@"http://online.dongting.com/recomm/new_albums?utdid=VH621NYGPzQDAMRYgXwVA2E8&page=1&size=10"];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [dic objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        [self.collectionView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark - 获取cell中的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReNewAlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    RecommendLatestModel *model = [self.array objectAtIndex:indexPath.row];
    NSString *str = model.pic;
    NSURL *imageUrl = [NSURL URLWithString:str];
    [cell.albumImageView sd_setImageWithURL:imageUrl placeholderImage:nil];
    
    return cell;
}

#pragma mark - 点击item之后进行的操作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendLatestModel *model = [self.array objectAtIndex:indexPath.row];
    // 创建对象并且push进去
    ReHotSongListViewController *hot = [[ReHotSongListViewController alloc] init];
    hot.str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?utdid=VH621NYGPzQDAMRYgXwVA2E8&msg_ids=[%@]", model.msg_id];
    
    [self.navigationController pushViewController:hot animated:YES];
    [hot release];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 300);
}

#pragma mark - 获取item的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}

@end
